options(scipen=9999)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(RSocrata)
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: spatstat.geom
## spatstat.geom 3.2-7
## Loading required package: spatstat.random
## spatstat.random 3.2-2
## Loading required package: spatstat.explore
## Loading required package: nlme
##
## Attaching package: 'nlme'
##
## The following object is masked from 'package:dplyr':
##
## collapse
##
## spatstat.explore 3.2-5
## Loading required package: spatstat.model
## Loading required package: rpart
## spatstat.model 3.2-8
## Loading required package: spatstat.linnet
## spatstat.linnet 3.1-3
##
## spatstat 3.0-7
## For an introduction to spatstat, type 'beginner'
library(viridis)
## Loading required package: viridisLite
library(FNN)
library(spdep)
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(gridExtra)
##
## Attaching package: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
shapefile <- st_read ("Neighbourhoods.geojson")
## Reading layer `Neighbourhoods' from data source
## `/Users/dongyoungkim/Library/Mobile Documents/com~apple~CloudDocs/03. Transition toronto/2024_Mar_Youth/Neighbourhoods.geojson'
## using driver `GeoJSON'
## Simple feature collection with 158 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -79.63926 ymin: 43.581 xmax: -79.11527 ymax: 43.85546
## Geodetic CRS: WGS 84
plot(shapefile)
## Warning: plotting the first 9 out of 11 attributes; use max.plot = 11 to plot
## all
which(shapefile$AREA_NAME == "Rexdale")
## integer(0)
table(shapefile$AREA_NAME)
##
## Agincourt North Agincourt South-Malvern West
## 1 1
## Alderwood Annex
## 1 1
## Avondale Banbury-Don Mills
## 1 1
## Bathurst Manor Bay-Cloverhill
## 1 1
## Bayview Village Bayview Woods-Steeles
## 1 1
## Bedford Park-Nortown Beechborough-Greenbrook
## 1 1
## Bendale South Bendale-Glen Andrew
## 1 1
## Birchcliffe-Cliffside Black Creek
## 1 1
## Blake-Jones Briar Hill-Belgravia
## 1 1
## Bridle Path-Sunnybrook-York Mills Broadview North
## 1 1
## Brookhaven-Amesbury Cabbagetown-South St.James Town
## 1 1
## Caledonia-Fairbank Casa Loma
## 1 1
## Centennial Scarborough Church-Wellesley
## 1 1
## Clairlea-Birchmount Clanton Park
## 1 1
## Cliffcrest Corso Italia-Davenport
## 1 1
## Danforth Danforth East York
## 1 1
## Don Valley Village Dorset Park
## 1 1
## Dovercourt Village Downsview
## 1 1
## Downtown Yonge East Dufferin Grove
## 1 1
## East End-Danforth East L'Amoreaux
## 1 1
## East Willowdale Edenbridge-Humber Valley
## 1 1
## Eglinton East Elms-Old Rexdale
## 1 1
## Englemount-Lawrence Eringate-Centennial-West Deane
## 1 1
## Etobicoke City Centre Etobicoke West Mall
## 1 1
## Fenside-Parkwoods Flemingdon Park
## 1 1
## Forest Hill North Forest Hill South
## 1 1
## Fort York-Liberty Village Glenfield-Jane Heights
## 1 1
## Golfdale-Cedarbrae-Woburn Greenwood-Coxwell
## 1 1
## Guildwood Harbourfront-CityPlace
## 1 1
## Henry Farm High Park North
## 1 1
## High Park-Swansea Highland Creek
## 1 1
## Hillcrest Village Humber Bay Shores
## 1 1
## Humber Heights-Westmount Humber Summit
## 1 1
## Humbermede Humewood-Cedarvale
## 1 1
## Ionview Islington
## 1 1
## Junction Area Junction-Wallace Emerson
## 1 1
## Keelesdale-Eglinton West Kennedy Park
## 1 1
## Kensington-Chinatown Kingsview Village-The Westway
## 1 1
## Kingsway South L'Amoreaux West
## 1 1
## Lambton Baby Point Lansing-Westgate
## 1 1
## Lawrence Park North Lawrence Park South
## 1 1
## Leaside-Bennington Little Portugal
## 1 1
## Long Branch Malvern East
## 1 1
## Malvern West Maple Leaf
## 1 1
## Markland Wood Milliken
## 1 1
## Mimico-Queensway Morningside
## 1 1
## Morningside Heights Moss Park
## 1 1
## Mount Dennis Mount Olive-Silverstone-Jamestown
## 1 1
## Mount Pleasant East New Toronto
## 1 1
## Newtonbrook East Newtonbrook West
## 1 1
## North Riverdale North St.James Town
## 1 1
## North Toronto O'Connor-Parkview
## 1 1
## Oakdale-Beverley Heights Oakridge
## 1 1
## Oakwood Village Old East York
## 1 1
## Palmerston-Little Italy Parkwoods-O'Connor Hills
## 1 1
## Pelmo Park-Humberlea Playter Estates-Danforth
## 1 1
## Pleasant View Princess-Rosethorn
## 1 1
## Regent Park Rexdale-Kipling
## 1 1
## Rockcliffe-Smythe Roncesvalles
## 1 1
## Rosedale-Moore Park Runnymede-Bloor West Village
## 1 1
## Rustic Scarborough Village
## 1 1
## South Eglinton-Davisville South Parkdale
## 1 1
## South Riverdale St Lawrence-East Bayfront-The Islands
## 1 1
## St.Andrew-Windfields Steeles
## 1 1
## Stonegate-Queensway Tam O'Shanter-Sullivan
## 1 1
## Taylor-Massey The Beaches
## 1 1
## Thistletown-Beaumond Heights Thorncliffe Park
## 1 1
## Trinity-Bellwoods University
## 1 1
## Victoria Village Wellington Place
## 1 1
## West Hill West Humber-Clairville
## 1 1
## West Queen West West Rouge
## 1 1
## Westminster-Branson Weston
## 1 1
## Weston-Pelham Park Wexford/Maryvale
## 1 1
## Willowdale West Willowridge-Martingrove-Richview
## 1 1
## Woburn North Woodbine Corridor
## 1 1
## Woodbine-Lumsden Wychwood
## 1 1
## Yonge-Bay Corridor Yonge-Doris
## 1 1
## Yonge-Eglinton Yonge-St.Clair
## 1 1
## York University Heights Yorkdale-Glen Park
## 1 1
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
shapefile_with_area_name <- ggplot(data = shapefile) +
geom_sf() +
geom_sf_text(aes(label = AREA_NAME), size = 3, check_overlap = TRUE) +
theme_minimal()
plotly_object <- ggplotly(shapefile_with_area_name)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
plotly_object